Nguồn: Mobile Price Classification
Tập dữ liệu train.csv này có 2000 dòng dữ liệu với 21 cột thuộc tính, gồm có:
battery_power: Tổng năng lượng mà pin có thể lưu trữ trong một thời gian tính bằng mAh.
blue: Có chức năng bluetooth hay không.
clock_speed: tốc độ mà bộ vi xử lý thực hiện các lệnh.
dual_sim: có hỗ trợ sim kép hay không.
fc: mega pixels của camera trước.
four_g: có hỗ trợ 4G hay không.
int_memory: dung lượng bộ nhớ trong tính bằng Gigabytes.
m_dept: độ sâu của điện thoại tính bằng cm.
mobile_wt: trọng lượng của điện thoại.
n_cores: số lõi của bộ xử lý.
pc: mega pixels của camera chính.
px_height: độ phân giải pixel của chiều cao điện thoại.
px_width: độ phân giải pixel của chiều rộng điện thoại.
ram: bộ nhớ truy cập ngẫu nhiên tính bằng Mega Byte.
sc_h: chiều cao màn hình của điện thoại tính bằng cm.
sc_w: chiều rộng màn hình của điện thoại tính bằng cm.
talk_time: thời gian sử dụng dài nhất sau một lần sạc đầy
three_g: có hỗ trợ 3G hay không.
touch_screen: có hỗ trợ màn hình cảm ứng hay không.
wifi: có hỗ trợ chức năng wifi hay không.
price_range: Đây là biến mục tiêu với giá trị 0(chi phí thấp), 1(chi phí trung bình), 2(chi phí cao) và 3(chi phí rất cao).
## Warning: package 'mice' was built under R version 4.3.2
## Warning: package 'fastDummies' was built under R version 4.3.2
## Warning: package 'caret' was built under R version 4.3.2
## Warning: package 'rpart.plot' was built under R version 4.3.2
## Warning: package 'viridis' was built under R version 4.3.2
## Warning: package 'e1071' was built under R version 4.3.2
## Warning: package 'ROCR' was built under R version 4.3.2
## Warning: package 'randomForest' was built under R version 4.3.2
## Warning: package 'readxl' was built under R version 4.3.2
## Warning: package 'factoextra' was built under R version 4.3.2
## battery_power blue clock_speed dual_sim fc four_g int_memory m_dep mobile_wt
## 1 842 0 2.2 0 1 0 7 0.6 188
## 2 1021 1 0.5 1 0 1 53 0.7 136
## 3 563 1 0.5 1 2 1 41 0.9 145
## 4 615 1 2.5 0 0 0 10 0.8 131
## 5 1821 1 1.2 0 13 1 44 0.6 141
## 6 1859 0 0.5 1 3 0 22 0.7 164
## n_cores pc px_height px_width ram sc_h sc_w talk_time three_g touch_screen
## 1 2 2 20 756 2549 9 7 19 0 0
## 2 3 6 905 1988 2631 17 3 7 1 1
## 3 5 6 1263 1716 2603 11 2 9 1 1
## 4 6 9 1216 1786 2769 16 8 11 1 0
## 5 2 14 1208 1212 1411 8 2 15 1 1
## 6 1 7 1004 1654 1067 17 1 10 1 0
## wifi price_range
## 1 1 1
## 2 0 2
## 3 0 2
## 4 0 2
## 5 0 1
## 6 0 1
## battery_power blue clock_speed dual_sim fc four_g int_memory m_dep
## 1995 858 0 2.2 0 1 0 50 0.1
## 1996 794 1 0.5 1 0 1 2 0.8
## 1997 1965 1 2.6 1 0 0 39 0.2
## 1998 1911 0 0.9 1 1 1 36 0.7
## 1999 1512 0 0.9 0 4 1 46 0.1
## 2000 510 1 2.0 1 5 1 45 0.9
## mobile_wt n_cores pc px_height px_width ram sc_h sc_w talk_time three_g
## 1995 84 1 2 528 1416 3978 17 16 3 1
## 1996 106 6 14 1222 1890 668 13 4 19 1
## 1997 187 4 3 915 1965 2032 11 10 16 1
## 1998 108 8 3 868 1632 3057 9 1 5 1
## 1999 145 5 5 336 670 869 18 10 19 1
## 2000 168 6 16 483 754 3919 19 4 2 1
## touch_screen wifi price_range
## 1995 1 0 3
## 1996 1 0 0
## 1997 1 1 2
## 1998 1 0 3
## 1999 1 1 0
## 2000 1 1 3
## 'data.frame': 2000 obs. of 21 variables:
## $ battery_power: int 842 1021 563 615 1821 1859 1821 1954 1445 509 ...
## $ blue : int 0 1 1 1 1 0 0 0 1 1 ...
## $ clock_speed : num 2.2 0.5 0.5 2.5 1.2 0.5 1.7 0.5 0.5 0.6 ...
## $ dual_sim : int 0 1 1 0 0 1 0 1 0 1 ...
## $ fc : int 1 0 2 0 13 3 4 0 0 2 ...
## $ four_g : int 0 1 1 0 1 0 1 0 0 1 ...
## $ int_memory : int 7 53 41 10 44 22 10 24 53 9 ...
## $ m_dep : num 0.6 0.7 0.9 0.8 0.6 0.7 0.8 0.8 0.7 0.1 ...
## $ mobile_wt : int 188 136 145 131 141 164 139 187 174 93 ...
## $ n_cores : int 2 3 5 6 2 1 8 4 7 5 ...
## $ pc : int 2 6 6 9 14 7 10 0 14 15 ...
## $ px_height : int 20 905 1263 1216 1208 1004 381 512 386 1137 ...
## $ px_width : int 756 1988 1716 1786 1212 1654 1018 1149 836 1224 ...
## $ ram : int 2549 2631 2603 2769 1411 1067 3220 700 1099 513 ...
## $ sc_h : int 9 17 11 16 8 17 13 16 17 19 ...
## $ sc_w : int 7 3 2 8 2 1 8 3 1 10 ...
## $ talk_time : int 19 7 9 11 15 10 18 5 20 12 ...
## $ three_g : int 0 1 1 1 1 1 1 1 1 1 ...
## $ touch_screen : int 0 1 1 0 1 0 0 1 0 0 ...
## $ wifi : int 1 0 0 0 0 0 1 1 0 0 ...
## $ price_range : int 1 2 2 2 1 1 3 0 0 0 ...
## battery_power blue clock_speed dual_sim
## Min. : 501.0 Min. :0.000 Min. :0.500 Min. :0.0000
## 1st Qu.: 851.8 1st Qu.:0.000 1st Qu.:0.700 1st Qu.:0.0000
## Median :1226.0 Median :0.000 Median :1.500 Median :1.0000
## Mean :1238.5 Mean :0.495 Mean :1.522 Mean :0.5095
## 3rd Qu.:1615.2 3rd Qu.:1.000 3rd Qu.:2.200 3rd Qu.:1.0000
## Max. :1998.0 Max. :1.000 Max. :3.000 Max. :1.0000
## fc four_g int_memory m_dep
## Min. : 0.000 Min. :0.0000 Min. : 2.00 Min. :0.1000
## 1st Qu.: 1.000 1st Qu.:0.0000 1st Qu.:16.00 1st Qu.:0.2000
## Median : 3.000 Median :1.0000 Median :32.00 Median :0.5000
## Mean : 4.309 Mean :0.5215 Mean :32.05 Mean :0.5018
## 3rd Qu.: 7.000 3rd Qu.:1.0000 3rd Qu.:48.00 3rd Qu.:0.8000
## Max. :19.000 Max. :1.0000 Max. :64.00 Max. :1.0000
## mobile_wt n_cores pc px_height
## Min. : 80.0 Min. :1.000 Min. : 0.000 Min. : 0.0
## 1st Qu.:109.0 1st Qu.:3.000 1st Qu.: 5.000 1st Qu.: 282.8
## Median :141.0 Median :4.000 Median :10.000 Median : 564.0
## Mean :140.2 Mean :4.521 Mean : 9.916 Mean : 645.1
## 3rd Qu.:170.0 3rd Qu.:7.000 3rd Qu.:15.000 3rd Qu.: 947.2
## Max. :200.0 Max. :8.000 Max. :20.000 Max. :1960.0
## px_width ram sc_h sc_w
## Min. : 500.0 Min. : 256 Min. : 5.00 Min. : 0.000
## 1st Qu.: 874.8 1st Qu.:1208 1st Qu.: 9.00 1st Qu.: 2.000
## Median :1247.0 Median :2146 Median :12.00 Median : 5.000
## Mean :1251.5 Mean :2124 Mean :12.31 Mean : 5.767
## 3rd Qu.:1633.0 3rd Qu.:3064 3rd Qu.:16.00 3rd Qu.: 9.000
## Max. :1998.0 Max. :3998 Max. :19.00 Max. :18.000
## talk_time three_g touch_screen wifi
## Min. : 2.00 Min. :0.0000 Min. :0.000 Min. :0.000
## 1st Qu.: 6.00 1st Qu.:1.0000 1st Qu.:0.000 1st Qu.:0.000
## Median :11.00 Median :1.0000 Median :1.000 Median :1.000
## Mean :11.01 Mean :0.7615 Mean :0.503 Mean :0.507
## 3rd Qu.:16.00 3rd Qu.:1.0000 3rd Qu.:1.000 3rd Qu.:1.000
## Max. :20.00 Max. :1.0000 Max. :1.000 Max. :1.000
## price_range
## Min. :0.00
## 1st Qu.:0.75
## Median :1.50
## Mean :1.50
## 3rd Qu.:2.25
## Max. :3.00
miss_rate<-function(){
for(i in 1:ncol(data))
print( paste(
colnames(data[i]),
mean(is.na(unlist(data[,i])) | unlist(data[,i]) == ''),
sep=':'))
}
miss_rate()## [1] "battery_power:0"
## [1] "blue:0"
## [1] "clock_speed:0"
## [1] "dual_sim:0"
## [1] "fc:0"
## [1] "four_g:0"
## [1] "int_memory:0"
## [1] "m_dep:0"
## [1] "mobile_wt:0"
## [1] "n_cores:0"
## [1] "pc:0"
## [1] "px_height:0"
## [1] "px_width:0"
## [1] "ram:0"
## [1] "sc_h:0"
## [1] "sc_w:0"
## [1] "talk_time:0"
## [1] "three_g:0"
## [1] "touch_screen:0"
## [1] "wifi:0"
## [1] "price_range:0"
Nhận xét: - Như vậy, tập dữ liệu train.csv là hoàn hảo, không có chứa các giá trị null nào.
unique_value_counts <- sapply(data, function(x) length(unique(x)))
result_df <- data.frame(Column = names(unique_value_counts), Number_of_unique_values = unique_value_counts)
print(result_df)## Column Number_of_unique_values
## battery_power battery_power 1094
## blue blue 2
## clock_speed clock_speed 26
## dual_sim dual_sim 2
## fc fc 20
## four_g four_g 2
## int_memory int_memory 63
## m_dep m_dep 10
## mobile_wt mobile_wt 121
## n_cores n_cores 8
## pc pc 21
## px_height px_height 1137
## px_width px_width 1109
## ram ram 1562
## sc_h sc_h 15
## sc_w sc_w 19
## talk_time talk_time 19
## three_g three_g 2
## touch_screen touch_screen 2
## wifi wifi 2
## price_range price_range 4
Nhận xét:
Các thuộc tính mang giá trị phân loại: blue, dual_sim, four_g, n_cores, three_g, touch_screen, wifi.
Các thuộc tính mang giá trị liên tục: battery_power, clock_speed, fc, int_memory, m_dep, mobile_wt, pc, px_height, px_width, ram, sc_h, sc_w, talk_time.
# Chọn các biến phân loại
categorical_columns <- c('blue', 'dual_sim', 'four_g', 'n_cores', 'three_g', 'touch_screen', 'wifi')
# Hàm phân tích đơn biến cho biến phân loại
categorical_univariate_analysis <- function(variable) {
df <- data.frame(value = data[[variable]])
# Biểu đồ pie chart
pie_chart <- ggplot(df, aes(x = "", fill = factor(value))) +
geom_bar(width = 1, position = "fill") +
coord_polar(theta = "y") +
geom_text(aes(label = scales::percent(..count../sum(..count..))),
stat = "count",
position = position_fill(vjust = 0.5),
color = "black",
size = 3) +
labs(title = paste("Pie Chart for", variable),
x = NULL,
y = NULL) +
theme_minimal() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
panel.grid = element_blank())
print(pie_chart)
}
# Áp dụng hàm cho từng biến phân loại
for (variable in categorical_columns) {
categorical_univariate_analysis(variable)
}## Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(count)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Nhận xét:
Đa số các biến phân loại có số lượng mẫu đồng đều.
Trừ chức năng hỗ trợ 3G thì số lượng điện thoại có chức năng này chiếm đến 76%
frequency_table <- table(data$price_range)
# Tạo DataFrame từ kết quả
frequency_df <- data.frame(labels = names(frequency_table), sizes = as.numeric(frequency_table))
# Biểu đồ tròn (Pie chart) với hiển thị giá trị
pie_plot <- ggplot(frequency_df, aes(x = "", y = sizes, fill = labels)) +
geom_bar(stat = "identity", width = 1, color = "white") +
coord_polar("y") +
scale_fill_manual(values = c("green", "pink", "magenta", "cyan")) +
theme_void() +
ggtitle("COSTS (Pie Chart)") +
geom_text(aes(label = sizes), position = position_stack(vjust = 0.5)) # Hiển thị giá trị
# Biểu đồ cột (Bar chart)
bar_plot <- ggplot(frequency_df, aes(x = labels, y = sizes, fill = labels)) +
geom_bar(stat = "identity", color = "white") +
scale_fill_manual(values = c("green", "pink", "magenta", "cyan")) +
ggtitle("COSTS (Bar Chart)") +
theme_minimal() +
theme(legend.position = "none")
ggarrange(pie_plot, bar_plot, nrow = 1, ncol = 2)Nhận xét:
# num_features chứa tên của các đặc trưng trong tập dữ liệu
num_features <- c("battery_power", "blue", "clock_speed", "dual_sim", "fc", "four_g", "int_memory", "m_dep", "mobile_wt", "n_cores", "pc", "px_height", "px_width", "ram", "sc_h", "sc_w", "talk_time", "three_g", "touch_screen", "wifi", "price_range")
# Tính ma trận tương quan
correlation_matrix <- cor(data[num_features])
# Sử dụng thư viện corrplot để tạo heatmap
corrplot(correlation_matrix, method = "color", type = "full", tl.col = "black", tl.srt = 45)Nhận xét:
# Tính ma trận tương quan
correlation_matrix <- cor(data[num_features])
# Trích xuất tương quan của mỗi đặc trưng với 'price_range'
price_corr <- correlation_matrix["price_range", ]
# Sắp xếp tương quan theo giá trị tuyệt đối giảm dần
sorted_price_corr <- sort(abs(price_corr), decreasing = TRUE)
barplot(sorted_price_corr, col = 'skyblue', main = "Tương quan giữa các đặc trưng và Price Range", cex.names = 0.8, las = 2)Nhận xét:
- Biểu đồ cho thấy mối quan hệ mạnh mẽ giữa giá trị “price_range” và thuộc tính “ram”. Dữ liệu cho thấy dung lượng bộ nhớ truy cập ngẫu nhiên càng cao, thì mức giá của điện thoại càng tăng. Ngoài ra, các thuộc tính “battery_power”, “px_height”, “px_width” cũng có mối quan hệ với “price_range”. Chúng ta sẽ sử dụng các thuộc tính này khi xây dựng mô hình để dự đoán “price_range”.
# Tạo biểu đồ histogram bằng ggplot2
ggplot(data, aes(x = ram, fill = as.factor(price_range))) +
geom_histogram(binwidth = 1, position = "identity", alpha = 0.7) +
facet_wrap(~price_range, scales = "free_y") +
labs(x = "RAM", y = "Count") +
theme_minimal()Nhận xét:
num_features02 <- c("battery_power", "clock_speed", "fc", "int_memory", "m_dep", "mobile_wt", "n_cores", "pc", "px_height", "px_width", "ram", "sc_h", "sc_w", "talk_time", "price_range")
# Thiết lập bảng đồ đa panel
n_rows <- 4
n_cols <- 5
par(mfrow=c(n_rows, n_cols), mar=c(4, 4, 2, 1), oma=c(1, 1, 2, 1))
#khúc này chọn màu
colors <- viridis(15)
# Lặp qua từng đặc trưng số và vẽ biểu đồ histogram với màu sắc tương tự
for (i in 1:length(num_features02)) {
hist(data[[num_features02[i]]], main=num_features02[i], col=colors[i], xlab=num_features02[i])
}
# Đặt tiêu đề cho toàn bảng đồ
title <- "Phân phối đặc trưng"
mtext(title, outer=TRUE, line=0.5, cex=1.2)
# Đặt kích thước biểu đồ và kết thúc
par(mfrow=c(1, 1), mar=c(5, 4, 4, 2) + 0.1)Nhận xét: - Có một số đặc trưng có sự phân bố lệch như: fc, px_height, sc_w khi giá trị phân phối giảm dần đều. - Ngược lại,có một số trưng có phân phối đồng đều như: battery_power, int_memory, ram, …
long_data <- reshape2::melt(data, id.vars = "price_range", measure.vars = num_features02)
ggplot(long_data, aes(x = value, fill = factor(price_range))) +
geom_density(alpha = 0.5) +
facet_wrap(~variable, scales = "free") +
labs(title = "Phân phối đặc trưng với Price Range") +
theme_minimal()Nhận xét:
# chuyển đổi dữ liệu sang định dạng long
long_data <- reshape2::melt(data, id.vars = "price_range", measure.vars = "battery_power")
# Vẽ boxplot
ggplot(long_data, aes(x = factor(price_range), y = value, fill = factor(price_range))) +
geom_boxplot() +
labs(x = "Price Range", y = "Battery Power", title = "Battery Power vs Price Range", fontface = "bold") +
theme_minimal()ggplot(data, aes(x = factor(price_range), y = ram, fill = factor(price_range))) +
geom_boxplot() +
labs(title = "Boxplot for RAM with Price Range") +
ylab("RAM") +
xlab("Price Range") +
theme_minimal()ggplot(data, aes(x = factor(price_range), y = px_width, fill = factor(price_range))) +
geom_boxplot() +
labs(title = "Boxplot for Pixel Width with Price Range") +
ylab("Pixel Width") +
xlab("Price Range") +
theme_minimal()ggplot(data, aes(x = factor(price_range), y = px_height, fill = factor(price_range))) +
geom_boxplot() +
labs(title = "Boxplot for Pixel Height with Price Range") +
ylab("Pixel Height") +
xlab("Price Range") +
theme_minimal()Nhận xét:
## price_range với ram và battery_power
ggplot(data, aes(x = ram, y = battery_power, color = factor(price_range))) +
geom_point() +
labs(title = "price_range with ram and battery_power") +
theme_minimal()## price_range với ram và px_width
ggplot(data, aes(x = ram, y = px_width, color = factor(price_range))) +
geom_point() +
labs(title = "price_range with ram and px_width") +
theme_minimal()## price_range với ram và px_height
ggplot(data, aes(x = ram, y = px_height, color = factor(price_range))) +
geom_point() +
labs(title = "price_range with ram and px_height") +
theme_minimal()## price_range với px_width và px_height
ggplot(data, aes(x = px_width, y = px_height, color = factor(price_range))) +
geom_point() +
labs(title = "price_range with px_width and px_height") +
theme_minimal()Nhận xét:
##
## Call:
## lm(formula = mlrm_df_train$price_range ~ ., data = mlrm_df_train)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.86129 -0.24233 0.00567 0.23298 0.80975
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.663e+00 4.032e-02 -41.24 <2e-16 ***
## battery_power 5.091e-04 1.993e-05 25.55 <2e-16 ***
## px_height 3.008e-04 2.303e-05 13.06 <2e-16 ***
## px_width 2.554e-04 2.356e-05 10.84 <2e-16 ***
## ram 9.478e-04 8.094e-06 117.10 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.32 on 1327 degrees of freedom
## Multiple R-squared: 0.9184, Adjusted R-squared: 0.9181
## F-statistic: 3733 on 4 and 1327 DF, p-value: < 2.2e-16
# dự báo giá trị của price_range trong bộ mẫu xây dựng, sử dụng hàm predict
predict_train = predict(mlrm_train, mlrm_df_train)
# tổng phương sai của mô hình dự báo
sum_variance_DuBao_train = sum((mlrm_df_train$price_range - predict_train)^2)
# tổng phương sai của dữ liệu ban đầu
sum_variance_DuLieuBanDau_train = sum((mean(mlrm_df_train$price_range) - predict_train)^2)## [1] 0.9111388
predict_test = predict(mlrm_train, mlrm_df_test)
sum_variance_DuBao_test = sum((mlrm_df_test$price_range - predict_test)^2)
sum_variance_DuLieuBanDau_test = sum((mean(mlrm_df_test$price_range) - predict_test)^2)
R2_mlrm_test = 1 - (sum_variance_DuBao_test/sum_variance_DuLieuBanDau_test)
cat("R2_mlrm_test:", R2_mlrm_test, "\n")## R2_mlrm_test: 0.906573
# Tính RMSE
RMSE_mlrm_test <- sqrt(sum((mlrm_df_test$price_range - predict_test)^2) / nrow(mlrm_df_test))
# In giá trị RMSE
cat("RMSE_mlrm_test:", RMSE_mlrm_test, "\n")## RMSE_mlrm_test: 0.3270762
Nhận xét:
=> Mô hình có sự chính xác cao.
# Lựa chọn đặc trưng và mục tiêu
rt_features <- c("battery_power", "px_height", "px_width", "ram")
rt_target <- "price_range"## n= 1600
##
## node), split, n, deviance, yval
## * denotes terminal node
##
## 1) root 1600 2000.00000 1.5000000
## 2) ram< 2247 840 368.62380 0.6261905
## 4) ram< 1349.5 470 86.42766 0.2191489 *
## 5) ram>=1349.5 370 105.40810 1.1432430
## 10) battery_power< 1095 153 30.83660 0.8562092 *
## 11) battery_power>=1095 217 53.07834 1.3456220 *
## 3) ram>=2247 760 281.11050 2.4657890
## 6) ram< 3013.5 338 98.85503 2.0207100
## 12) battery_power< 1359 205 46.32195 1.8195120 *
## 13) battery_power>=1359 133 31.44361 2.3308270 *
## 7) ram>=3013.5 422 61.67062 2.8222750 *
# Tính R^2
r_squared <- 1 - sum((test_data$price_range - predictions)^2) / sum((test_data$price_range - mean(test_data$price_range))^2)
cat("R-squared:", round(r_squared, 4), "\n")## R-squared: 0.8192
# Đánh giá hiệu suất ( RMSE)
rmse_RT <- sqrt(mean((predictions - test_data$price_range)^2))
print(paste("Root Mean Squared Error (RMSE):", rmse_RT))## [1] "Root Mean Squared Error (RMSE): 0.475402332874267"
mae_RT <- mean(abs(predictions - test_data$price_range))
print(paste("Mean Absolute Error (MAE):", mae_RT))## [1] "Mean Absolute Error (MAE): 0.376819158213237"
Nhận xét:
Classification_tree <- rpart(price_range~ ., data = train_data, method = "class")
rpart.plot(Classification_tree, extra = 100)## n= 1600
##
## node), split, n, loss, yval, (yprob)
## * denotes terminal node
##
## 1) root 1600 1200 0 (0.250000000 0.250000000 0.250000000 0.250000000)
## 2) ram< 2247 840 440 0 (0.476190476 0.421428571 0.102380952 0.000000000)
## 4) ram< 1106 359 41 0 (0.885793872 0.114206128 0.000000000 0.000000000) *
## 5) ram>=1106 481 168 1 (0.170478170 0.650727651 0.178794179 0.000000000)
## 10) ram< 1473.5 172 72 1 (0.395348837 0.581395349 0.023255814 0.000000000)
## 20) battery_power< 1115 76 16 0 (0.789473684 0.210526316 0.000000000 0.000000000) *
## 21) battery_power>=1115 96 12 1 (0.083333333 0.875000000 0.041666667 0.000000000) *
## 11) ram>=1473.5 309 96 1 (0.045307443 0.689320388 0.265372168 0.000000000)
## 22) battery_power< 1435.5 196 36 1 (0.071428571 0.816326531 0.112244898 0.000000000) *
## 23) battery_power>=1435.5 113 53 2 (0.000000000 0.469026549 0.530973451 0.000000000)
## 46) px_width< 1102.5 42 7 1 (0.000000000 0.833333333 0.166666667 0.000000000) *
## 47) px_width>=1102.5 71 18 2 (0.000000000 0.253521127 0.746478873 0.000000000) *
## 3) ram>=2247 760 360 3 (0.000000000 0.060526316 0.413157895 0.526315789)
## 6) ram< 3123 376 119 2 (0.000000000 0.122340426 0.683510638 0.194148936)
## 12) battery_power< 1354 222 56 2 (0.000000000 0.202702703 0.747747748 0.049549550) *
## 13) battery_power>=1354 154 63 2 (0.000000000 0.006493506 0.590909091 0.402597403)
## 26) px_width< 1281.5 89 15 2 (0.000000000 0.011235955 0.831460674 0.157303371) *
## 27) px_width>=1281.5 65 17 3 (0.000000000 0.000000000 0.261538462 0.738461538) *
## 7) ram>=3123 384 57 3 (0.000000000 0.000000000 0.148437500 0.851562500) *
## NULL
# Hiển thị confusion matrix
conf_matrix <- table(prediction, test_data$price_range)
print(conf_matrix)##
## prediction 0 1 2 3
## 0 94 10 0 0
## 1 6 74 11 0
## 2 0 16 68 10
## 3 0 0 21 90
# Hiển thị tỉ lệ phân loại đúng trên tập kiểm tra
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix)
print(paste("Accuracy:", accuracy))## [1] "Accuracy: 0.815"
Nhận xét:
======================SVM=========================================
# Split into X and y
X <- Final_df[c('ram', 'n_cores', 'clock_speed')]
y <- Final_df$price_range
# Split into training and testing sets
set.seed(42)
index <- createDataPartition(y, p = 0.7, list = FALSE)
X_train <- X[index, ]
y_train <- y[index]
X_test <- X[-index, ]
y_test <- y[-index]
svm_model <- svm(X_train, y_train, kernel = "radial", type = "C", class.factor = 4)
# Predict on test set
y_pred <- predict(svm_model, X_test)
# Evaluate the model
conf_matrix <- table(y_pred, y_test)
accuracy_SVM <- sum(diag(conf_matrix)) / sum(conf_matrix)
# Display the results
print("Confusion Matrix:")## [1] "Confusion Matrix:"
## y_test
## y_pred 0 1 2 3
## 0 136 20 0 0
## 1 14 101 17 0
## 2 0 29 101 24
## 3 0 0 32 126
## [1] "Accuracy: 0.773333333333333"
## Factor w/ 4 levels "0","1","2","3": 2 3 3 3 2 2 4 1 1 1 ...
model_RandomForest <- randomForest(price_range ~ . - price_range, data = train, ntree = 1000, mtry = 7)
model_RandomForest##
## Call:
## randomForest(formula = price_range ~ . - price_range, data = train, ntree = 1000, mtry = 7)
## Type of random forest: classification
## Number of trees: 1000
## No. of variables tried at each split: 7
##
## OOB estimate of error rate: 10.12%
## Confusion matrix:
## 0 1 2 3 class.error
## 0 379 22 0 0 0.05486284
## 1 29 352 26 0 0.13513514
## 2 0 33 346 23 0.13930348
## 3 0 0 29 361 0.07435897
##
## prediction_RF 0 1 2 3
## 0 94 4 0 0
## 1 5 83 10 0
## 2 0 6 80 15
## 3 0 0 8 95
## 3 7 15 21 22 25 43 47 50 53 57 62 65 66 68 74
## 2 3 0 2 3 2 1 3 3 3 0 0 2 3 3 3
## 82 87 97 103 107 109 110 111 116 125 128 131 133 146 147 149
## 2 0 0 1 2 3 3 1 3 2 1 3 2 3 1 2
## 157 164 169 173 176 192 219 220 222 231 244 245 247 253 257 260
## 0 0 2 2 2 2 1 3 0 3 1 3 0 1 0 3
## 263 268 273 285 288 295 298 301 302 305 308 313 322 324 334 336
## 0 1 3 2 3 0 1 3 0 0 2 3 3 0 1 2
## 338 340 341 342 361 368 369 370 371 377 388 391 395 399 404 410
## 0 2 3 1 1 2 2 1 3 0 2 3 0 1 2 2
## 414 416 417 418 427 429 437 439 440 442 454 472 476 478 483 485
## 2 2 1 1 0 1 3 3 0 1 0 3 2 0 0 1
## 497 509 514 518 520 527 540 543 552 556 561 562 568 570 571 573
## 3 2 1 3 3 2 1 2 0 0 2 1 1 3 3 3
## 574 582 590 594 596 600 607 609 614 616 629 632 644 647 661 663
## 3 3 0 3 0 0 2 3 0 2 0 3 2 0 0 1
## 664 676 687 691 692 704 718 721 733 734 737 738 745 749 760 762
## 1 2 2 3 0 3 1 2 0 2 0 2 2 2 2 0
## 763 769 770 772 773 777 790 799 801 806 810 811 816 824 826 832
## 0 3 1 0 1 1 3 3 1 1 3 0 3 1 3 2
## 837 838 849 852 853 854 863 864 865 871 874 875 880 881 885 889
## 3 2 0 2 1 0 2 0 2 3 1 1 3 0 2 3
## 890 903 906 908 915 921 922 935 936 939 941 942 954 959 961 962
## 1 1 3 0 3 0 1 2 2 2 3 0 0 0 2 3
## 970 979 994 1007 1009 1010 1012 1018 1022 1027 1035 1036 1037 1039 1040 1041
## 1 3 0 0 2 1 0 2 3 0 0 0 3 1 1 0
## 1046 1055 1058 1061 1067 1086 1090 1097 1099 1102 1104 1106 1114 1126 1127 1145
## 2 1 2 3 3 2 1 3 3 1 3 0 1 2 1 1
## 1155 1161 1164 1171 1174 1175 1178 1180 1184 1190 1192 1196 1200 1206 1211 1219
## 0 2 2 0 3 2 2 3 0 2 3 2 2 1 3 2
## 1226 1229 1231 1236 1237 1238 1239 1245 1257 1259 1265 1266 1271 1278 1281 1284
## 0 1 0 0 0 0 2 1 3 3 1 3 0 0 1 2
## 1293 1294 1297 1299 1303 1308 1309 1324 1331 1340 1343 1344 1348 1350 1352 1368
## 1 1 2 2 0 1 3 1 2 3 1 2 2 3 1 2
## 1369 1381 1383 1385 1389 1392 1393 1394 1407 1418 1424 1429 1430 1431 1434 1443
## 0 2 0 1 3 1 0 0 1 1 3 1 1 1 3 1
## 1455 1465 1468 1475 1481 1483 1493 1494 1497 1499 1502 1506 1515 1518 1521 1524
## 1 0 3 3 1 2 0 0 0 1 2 0 1 2 3 0
## 1526 1527 1530 1535 1536 1539 1545 1548 1549 1552 1553 1554 1558 1564 1565 1578
## 3 2 1 3 2 2 2 3 2 1 2 0 2 2 0 1
## 1581 1583 1588 1590 1605 1620 1621 1635 1636 1639 1641 1647 1650 1662 1664 1666
## 0 0 1 3 0 1 3 0 3 2 1 1 1 0 3 1
## 1674 1679 1691 1692 1703 1708 1709 1713 1715 1717 1721 1722 1723 1730 1741 1745
## 1 2 3 1 2 1 3 2 2 2 2 0 2 3 3 1
## 1764 1772 1774 1775 1781 1782 1784 1794 1795 1797 1803 1804 1807 1819 1820 1821
## 2 1 0 2 0 3 0 1 3 1 3 3 1 0 2 0
## 1828 1838 1839 1840 1852 1859 1860 1861 1870 1874 1882 1893 1894 1905 1907 1909
## 3 1 0 2 3 2 0 0 3 0 1 0 3 3 1 3
## 1915 1916 1922 1927 1931 1934 1935 1937 1945 1958 1959 1972 1975 1981 1988 1993
## 0 2 2 1 3 1 2 3 1 3 2 1 1 1 0 0
## Levels: 0 1 2 3
## prediction_RF
## 3 3 3
## 7 4 4
## 15 1 1
## 21 3 4
## 22 4 4
## 25 3 2
## 43 2 2
## 47 4 4
## 50 4 4
## 53 4 4
## 57 1 1
## 62 1 1
## 65 3 3
## 66 4 4
## 68 4 4
## 74 4 4
## 82 3 3
## 87 1 1
## 97 1 1
## 103 2 2
## 107 3 4
## 109 4 4
## 110 4 4
## 111 2 2
## 116 4 4
## 125 3 3
## 128 2 2
## 131 4 4
## 133 3 3
## 146 4 4
## 147 2 2
## 149 3 3
## 157 1 1
## 164 1 1
## 169 3 4
## 173 3 3
## 176 3 3
## 192 3 3
## 219 2 2
## 220 4 4
## 222 1 1
## 231 4 4
## 244 2 3
## 245 4 4
## 247 1 1
## 253 2 2
## 257 1 1
## 260 4 4
## 263 1 1
## 268 2 2
## 273 4 4
## 285 3 3
## 288 4 4
## 295 1 1
## 298 2 2
## 301 4 4
## 302 1 1
## 305 1 1
## 308 3 3
## 313 4 4
## 322 4 4
## 324 1 1
## 334 2 2
## 336 3 3
## 338 1 1
## 340 3 3
## 341 4 3
## 342 2 2
## 361 2 2
## 368 3 4
## 369 3 3
## 370 2 2
## 371 4 4
## 377 1 1
## 388 3 2
## 391 4 4
## 395 1 2
## 399 2 2
## 404 3 3
## 410 3 3
## 414 3 3
## 416 3 3
## 417 2 2
## 418 2 2
## 427 1 1
## 429 2 2
## 437 4 4
## 439 4 4
## 440 1 1
## 442 2 2
## 454 1 1
## 472 4 4
## 476 3 3
## 478 1 1
## 483 1 1
## 485 2 2
## 497 4 4
## 509 3 3
## 514 2 2
## 518 4 4
## 520 4 4
## 527 3 3
## 540 2 2
## 543 3 2
## 552 1 2
## 556 1 1
## 561 3 4
## 562 2 2
## 568 2 2
## 570 4 4
## 571 4 4
## 573 4 4
## 574 4 4
## 582 4 4
## 590 1 1
## 594 4 4
## 596 1 1
## 600 1 1
## 607 3 3
## 609 4 3
## 614 1 1
## 616 3 3
## 629 1 1
## 632 4 4
## 644 3 3
## 647 1 1
## 661 1 1
## 663 2 2
## 664 2 3
## 676 3 3
## 687 3 3
## 691 4 4
## 692 1 1
## 704 4 4
## 718 2 2
## 721 3 4
## 733 1 1
## 734 3 4
## 737 1 1
## 738 3 3
## 745 3 3
## 749 3 3
## 760 3 4
## 762 1 1
## 763 1 1
## 769 4 4
## 770 2 2
## 772 1 1
## 773 2 2
## 777 2 1
## 790 4 4
## 799 4 4
## 801 2 2
## 806 2 3
## 810 4 4
## 811 1 1
## 816 4 3
## 824 2 2
## 826 4 4
## 832 3 4
## 837 4 4
## 838 3 3
## 849 1 1
## 852 3 3
## 853 2 2
## 854 1 1
## 863 3 3
## 864 1 1
## 865 3 3
## 871 4 4
## 874 2 1
## 875 2 2
## 880 4 4
## 881 1 1
## 885 3 3
## 889 4 4
## 890 2 2
## 903 2 3
## 906 4 4
## 908 1 1
## 915 4 4
## 921 1 1
## 922 2 2
## 935 3 3
## 936 3 4
## 939 3 3
## 941 4 4
## 942 1 1
## 954 1 1
## 959 1 2
## 961 3 3
## 962 4 4
## 970 2 2
## 979 4 4
## 994 1 1
## 1007 1 1
## 1009 3 4
## 1010 2 2
## 1012 1 1
## 1018 3 4
## 1022 4 4
## 1027 1 1
## 1035 1 1
## 1036 1 1
## 1037 4 4
## 1039 2 2
## 1040 2 2
## 1041 1 1
## 1046 3 3
## 1055 2 2
## 1058 3 3
## 1061 4 4
## 1067 4 4
## 1086 3 3
## 1090 2 2
## 1097 4 4
## 1099 4 4
## 1102 2 2
## 1104 4 4
## 1106 1 1
## 1114 2 2
## 1126 3 3
## 1127 2 2
## 1145 2 2
## 1155 1 1
## 1161 3 3
## 1164 3 3
## 1171 1 1
## 1174 4 4
## 1175 3 3
## 1178 3 3
## 1180 4 4
## 1184 1 1
## 1190 3 3
## 1192 4 4
## 1196 3 3
## 1200 3 3
## 1206 2 2
## 1211 4 3
## 1219 3 3
## 1226 1 1
## 1229 2 2
## 1231 1 1
## 1236 1 1
## 1237 1 1
## 1238 1 1
## 1239 3 3
## 1245 2 2
## 1257 4 4
## 1259 4 4
## 1265 2 2
## 1266 4 4
## 1271 1 1
## 1278 1 1
## 1281 2 2
## 1284 3 3
## 1293 2 1
## 1294 2 3
## 1297 3 3
## 1299 3 3
## 1303 1 1
## 1308 2 1
## 1309 4 4
## 1324 2 2
## 1331 3 3
## 1340 4 4
## 1343 2 2
## 1344 3 3
## 1348 3 3
## 1350 4 4
## 1352 2 2
## 1368 3 3
## 1369 1 1
## 1381 3 4
## 1383 1 1
## 1385 2 2
## 1389 4 4
## 1392 2 2
## 1393 1 1
## 1394 1 1
## 1407 2 2
## 1418 2 2
## 1424 4 4
## 1429 2 3
## 1430 2 2
## 1431 2 2
## 1434 4 4
## 1443 2 2
## 1455 2 2
## 1465 1 1
## 1468 4 4
## 1475 4 4
## 1481 2 2
## 1483 3 2
## 1493 1 1
## 1494 1 1
## 1497 1 1
## 1499 2 2
## 1502 3 3
## 1506 1 1
## 1515 2 2
## 1518 3 4
## 1521 4 4
## 1524 1 1
## 1526 4 3
## 1527 3 3
## 1530 2 2
## 1535 4 3
## 1536 3 3
## 1539 3 3
## 1545 3 3
## 1548 4 3
## 1549 3 3
## 1552 2 2
## 1553 3 3
## 1554 1 1
## 1558 3 3
## 1564 3 2
## 1565 1 1
## 1578 2 2
## 1581 1 1
## 1583 1 1
## 1588 2 2
## 1590 4 4
## 1605 1 1
## 1620 2 3
## 1621 4 3
## 1635 1 1
## 1636 4 4
## 1639 3 3
## 1641 2 3
## 1647 2 2
## 1650 2 2
## 1662 1 2
## 1664 4 4
## 1666 2 2
## 1674 2 2
## 1679 3 3
## 1691 4 4
## 1692 2 2
## 1703 3 3
## 1708 2 2
## 1709 4 4
## 1713 3 3
## 1715 3 3
## 1717 3 3
## 1721 3 3
## 1722 1 1
## 1723 3 3
## 1730 4 4
## 1741 4 4
## 1745 2 2
## 1764 3 3
## 1772 2 3
## 1774 1 1
## 1775 3 3
## 1781 1 1
## 1782 4 4
## 1784 1 1
## 1794 2 2
## 1795 4 4
## 1797 2 2
## 1803 4 4
## 1804 4 4
## 1807 2 2
## 1819 1 1
## 1820 3 3
## 1821 1 1
## 1828 4 4
## 1838 2 2
## 1839 1 1
## 1840 3 3
## 1852 4 4
## 1859 3 3
## 1860 1 1
## 1861 1 1
## 1870 4 4
## 1874 1 1
## 1882 2 3
## 1893 1 1
## 1894 4 4
## 1905 4 4
## 1907 2 2
## 1909 4 4
## 1915 1 1
## 1916 3 3
## 1922 3 2
## 1927 2 2
## 1931 4 4
## 1934 2 2
## 1935 3 3
## 1937 4 4
## 1945 2 1
## 1958 4 4
## 1959 3 4
## 1972 2 2
## 1975 2 2
## 1981 2 2
## 1988 1 1
## 1993 1 1
## pred real
## 3 3 3
## 7 4 4
## 15 1 1
## 21 3 4
## 22 4 4
## 25 3 2
## 43 2 2
## 47 4 4
## 50 4 4
## 53 4 4
## 57 1 1
## 62 1 1
## 65 3 3
## 66 4 4
## 68 4 4
## 74 4 4
## 82 3 3
## 87 1 1
## 97 1 1
## 103 2 2
## 107 3 4
## 109 4 4
## 110 4 4
## 111 2 2
## 116 4 4
## 125 3 3
## 128 2 2
## 131 4 4
## 133 3 3
## 146 4 4
## 147 2 2
## 149 3 3
## 157 1 1
## 164 1 1
## 169 3 4
## 173 3 3
## 176 3 3
## 192 3 3
## 219 2 2
## 220 4 4
## 222 1 1
## 231 4 4
## 244 2 3
## 245 4 4
## 247 1 1
## 253 2 2
## 257 1 1
## 260 4 4
## 263 1 1
## 268 2 2
## 273 4 4
## 285 3 3
## 288 4 4
## 295 1 1
## 298 2 2
## 301 4 4
## 302 1 1
## 305 1 1
## 308 3 3
## 313 4 4
## 322 4 4
## 324 1 1
## 334 2 2
## 336 3 3
## 338 1 1
## 340 3 3
## 341 4 3
## 342 2 2
## 361 2 2
## 368 3 4
## 369 3 3
## 370 2 2
## 371 4 4
## 377 1 1
## 388 3 2
## 391 4 4
## 395 1 2
## 399 2 2
## 404 3 3
## 410 3 3
## 414 3 3
## 416 3 3
## 417 2 2
## 418 2 2
## 427 1 1
## 429 2 2
## 437 4 4
## 439 4 4
## 440 1 1
## 442 2 2
## 454 1 1
## 472 4 4
## 476 3 3
## 478 1 1
## 483 1 1
## 485 2 2
## 497 4 4
## 509 3 3
## 514 2 2
## 518 4 4
## 520 4 4
## 527 3 3
## 540 2 2
## 543 3 2
## 552 1 2
## 556 1 1
## 561 3 4
## 562 2 2
## 568 2 2
## 570 4 4
## 571 4 4
## 573 4 4
## 574 4 4
## 582 4 4
## 590 1 1
## 594 4 4
## 596 1 1
## 600 1 1
## 607 3 3
## 609 4 3
## 614 1 1
## 616 3 3
## 629 1 1
## 632 4 4
## 644 3 3
## 647 1 1
## 661 1 1
## 663 2 2
## 664 2 3
## 676 3 3
## 687 3 3
## 691 4 4
## 692 1 1
## 704 4 4
## 718 2 2
## 721 3 4
## 733 1 1
## 734 3 4
## 737 1 1
## 738 3 3
## 745 3 3
## 749 3 3
## 760 3 4
## 762 1 1
## 763 1 1
## 769 4 4
## 770 2 2
## 772 1 1
## 773 2 2
## 777 2 1
## 790 4 4
## 799 4 4
## 801 2 2
## 806 2 3
## 810 4 4
## 811 1 1
## 816 4 3
## 824 2 2
## 826 4 4
## 832 3 4
## 837 4 4
## 838 3 3
## 849 1 1
## 852 3 3
## 853 2 2
## 854 1 1
## 863 3 3
## 864 1 1
## 865 3 3
## 871 4 4
## 874 2 1
## 875 2 2
## 880 4 4
## 881 1 1
## 885 3 3
## 889 4 4
## 890 2 2
## 903 2 3
## 906 4 4
## 908 1 1
## 915 4 4
## 921 1 1
## 922 2 2
## 935 3 3
## 936 3 4
## 939 3 3
## 941 4 4
## 942 1 1
## 954 1 1
## 959 1 2
## 961 3 3
## 962 4 4
## 970 2 2
## 979 4 4
## 994 1 1
## 1007 1 1
## 1009 3 4
## 1010 2 2
## 1012 1 1
## 1018 3 4
## 1022 4 4
## 1027 1 1
## 1035 1 1
## 1036 1 1
## 1037 4 4
## 1039 2 2
## 1040 2 2
## 1041 1 1
## 1046 3 3
## 1055 2 2
## 1058 3 3
## 1061 4 4
## 1067 4 4
## 1086 3 3
## 1090 2 2
## 1097 4 4
## 1099 4 4
## 1102 2 2
## 1104 4 4
## 1106 1 1
## 1114 2 2
## 1126 3 3
## 1127 2 2
## 1145 2 2
## 1155 1 1
## 1161 3 3
## 1164 3 3
## 1171 1 1
## 1174 4 4
## 1175 3 3
## 1178 3 3
## 1180 4 4
## 1184 1 1
## 1190 3 3
## 1192 4 4
## 1196 3 3
## 1200 3 3
## 1206 2 2
## 1211 4 3
## 1219 3 3
## 1226 1 1
## 1229 2 2
## 1231 1 1
## 1236 1 1
## 1237 1 1
## 1238 1 1
## 1239 3 3
## 1245 2 2
## 1257 4 4
## 1259 4 4
## 1265 2 2
## 1266 4 4
## 1271 1 1
## 1278 1 1
## 1281 2 2
## 1284 3 3
## 1293 2 1
## 1294 2 3
## 1297 3 3
## 1299 3 3
## 1303 1 1
## 1308 2 1
## 1309 4 4
## 1324 2 2
## 1331 3 3
## 1340 4 4
## 1343 2 2
## 1344 3 3
## 1348 3 3
## 1350 4 4
## 1352 2 2
## 1368 3 3
## 1369 1 1
## 1381 3 4
## 1383 1 1
## 1385 2 2
## 1389 4 4
## 1392 2 2
## 1393 1 1
## 1394 1 1
## 1407 2 2
## 1418 2 2
## 1424 4 4
## 1429 2 3
## 1430 2 2
## 1431 2 2
## 1434 4 4
## 1443 2 2
## 1455 2 2
## 1465 1 1
## 1468 4 4
## 1475 4 4
## 1481 2 2
## 1483 3 2
## 1493 1 1
## 1494 1 1
## 1497 1 1
## 1499 2 2
## 1502 3 3
## 1506 1 1
## 1515 2 2
## 1518 3 4
## 1521 4 4
## 1524 1 1
## 1526 4 3
## 1527 3 3
## 1530 2 2
## 1535 4 3
## 1536 3 3
## 1539 3 3
## 1545 3 3
## 1548 4 3
## 1549 3 3
## 1552 2 2
## 1553 3 3
## 1554 1 1
## 1558 3 3
## 1564 3 2
## 1565 1 1
## 1578 2 2
## 1581 1 1
## 1583 1 1
## 1588 2 2
## 1590 4 4
## 1605 1 1
## 1620 2 3
## 1621 4 3
## 1635 1 1
## 1636 4 4
## 1639 3 3
## 1641 2 3
## 1647 2 2
## 1650 2 2
## 1662 1 2
## 1664 4 4
## 1666 2 2
## 1674 2 2
## 1679 3 3
## 1691 4 4
## 1692 2 2
## 1703 3 3
## 1708 2 2
## 1709 4 4
## 1713 3 3
## 1715 3 3
## 1717 3 3
## 1721 3 3
## 1722 1 1
## 1723 3 3
## 1730 4 4
## 1741 4 4
## 1745 2 2
## 1764 3 3
## 1772 2 3
## 1774 1 1
## 1775 3 3
## 1781 1 1
## 1782 4 4
## 1784 1 1
## 1794 2 2
## 1795 4 4
## 1797 2 2
## 1803 4 4
## 1804 4 4
## 1807 2 2
## 1819 1 1
## 1820 3 3
## 1821 1 1
## 1828 4 4
## 1838 2 2
## 1839 1 1
## 1840 3 3
## 1852 4 4
## 1859 3 3
## 1860 1 1
## 1861 1 1
## 1870 4 4
## 1874 1 1
## 1882 2 3
## 1893 1 1
## 1894 4 4
## 1905 4 4
## 1907 2 2
## 1909 4 4
## 1915 1 1
## 1916 3 3
## 1922 3 2
## 1927 2 2
## 1931 4 4
## 1934 2 2
## 1935 3 3
## 1937 4 4
## 1945 2 1
## 1958 4 4
## 1959 3 4
## 1972 2 2
## 1975 2 2
## 1981 2 2
## 1988 1 1
## 1993 1 1
wcss <- numeric()
for (i in 1:10) {
kmeans.out <- kmeans(dataKMean_final, centers=i, nstart = 20)
wcss[i] <- kmeans.out$tot.withinss
}## Warning: did not converge in 10 iterations
## [1] 3998.0000 2483.0853 1548.4312 987.9987 832.8643 701.0726 586.6906
## [8] 495.6302 434.4454 395.6425
# Create cluster
clusters <- kmeans(dataKMean_final, centers = 3, iter.max = 100, nstart = 100)
# Visualize clusters using factoextra
#fviz_cluster(clusters, data = data2)
fviz_cluster(clusters, data = dataKMean_final, geom = "rect", show.clust.cent = FALSE)dataKMean <- dataKMean |> mutate(cluster = clusters$cluster)
dataKMean |> ggplot(aes(x = price_range, y = ram
, col = as.factor(cluster))) + geom_point()## Warning: package 'plotly' was built under R version 4.3.2
##
## Attaching package: 'plotly'
## The following object is masked from 'package:reshape':
##
## rename
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
# Tạo dữ liệu
data_accuracy <- data.frame(
Model = c("Classification Tree", "Random Forest", "SVM"),
Values = c(accuracy, accuracy_RF, accuracy_SVM)
)
# Vẽ biểu đồ cột
ggplot(data_accuracy, aes(x = Model, y = Values, fill = Model)) +
geom_bar(stat = "identity", width = 0.5) +
labs(title = "Compare accuracy between models",
x = "Model",
y = "Accuracy") +
theme_minimal()# Tạo dữ liệu
data_RMSE <- data.frame( Model = c("Multiple Linear Regression",
"Regression Tree"), Values = c(RMSE_mlrm_test, rmse_RT) )
# Vẽ biểu đồ cột
ggplot(data_RMSE, aes(x = Model, y = Values, fill = Model)) +
geom_bar(stat = "identity", width = 0.5) + labs(title = "Compare RMSE
between Models", x = "Model", y = "RMSE") + theme_minimal()data2 = read.csv("./test.csv", head =TRUE)
prediction_RF_test_csv <- predict(model_RandomForest, newdata = data2)## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 3 3 2 3 1 3 3 1 3 0 3 3 0 0 2 0
## 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
## 2 1 3 2 1 3 1 1 3 0 2 0 3 0 2 0
## 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
## 3 0 0 1 3 1 2 1 1 2 0 0 0 1 0 3
## 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
## 1 2 1 0 3 0 3 1 3 1 1 3 3 2 0 2
## 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 1 1 1 2 1 2 1 2 2 3 3 0 2 0 2 3
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
## 1 3 3 0 3 0 3 1 3 0 1 2 2 0 2 1
## 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
## 0 2 1 2 1 0 0 3 1 2 0 1 2 3 3 3
## 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
## 1 3 3 3 3 1 3 0 0 3 2 1 2 0 3 2
## 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 3 1 0 2 1 1 3 1 2 0 3 2 1 3 1 2
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
## 2 3 3 2 2 3 2 3 0 0 2 2 3 3 3 3
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
## 2 2 3 3 3 3 1 0 3 0 0 0 1 1 0 1
## 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
## 0 0 1 2 0 0 0 1 2 2 2 1 0 0 0 0
## 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
## 0 3 1 0 2 2 2 3 1 2 2 3 3 2 2 1
## 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
## 0 0 1 2 0 2 3 3 0 2 0 3 2 2 3 0
## 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
## 0 1 0 3 0 1 0 2 2 1 3 0 3 0 3 1
## 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
## 2 0 0 2 1 3 3 3 1 1 3 0 0 2 3 3
## 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
## 1 3 1 1 3 2 1 2 3 3 3 1 0 1 2 3
## 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 2 1 3 2 0 3 0 1 2 0 0 3 2 3 3 2
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
## 1 3 3 2 3 2 2 1 1 0 2 3 1 0 0 3
## 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
## 0 3 0 1 2 0 2 3 1 3 2 2 1 2 0 0
## 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
## 0 1 3 2 0 0 0 3 2 0 3 3 1 2 3 2
## 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
## 3 1 3 3 2 2 3 3 3 0 3 0 3 1 3 1
## 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
## 3 3 0 1 1 3 1 3 1 3 0 0 0 0 2 0
## 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
## 0 2 1 1 2 3 2 0 1 0 0 3 2 0 3 1
## 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
## 2 2 1 2 3 1 1 2 2 1 2 0 1 1 0 3
## 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
## 2 1 0 1 0 0 1 1 0 0 0 2 2 3 2 3
## 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
## 0 3 0 3 0 1 1 1 1 0 3 2 3 3 1 3
## 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
## 1 3 1 3 2 1 2 2 1 1 0 0 0 1 2 1
## 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
## 1 3 2 0 2 3 0 0 3 1 1 0 3 2 3 0
## 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
## 3 0 2 3 2 3 0 2 0 2 2 0 1 1 0 0
## 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
## 1 1 1 3 3 3 2 3 1 2 2 3 3 3 2 0
## 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
## 2 2 2 2 1 0 2 2 0 0 0 3 1 1 2 2
## 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
## 2 0 3 0 2 2 0 3 0 2 3 0 1 1 3 3
## 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
## 1 1 2 3 2 0 2 1 2 0 3 3 1 2 2 2
## 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
## 3 0 1 2 3 1 3 2 3 1 1 1 0 3 1 0
## 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
## 3 2 3 2 0 3 3 3 2 3 3 1 2 1 2 3
## 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
## 3 1 0 1 1 2 2 1 0 0 2 2 3 2 0 2
## 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
## 1 3 3 0 1 3 0 2 1 0 0 0 2 1 0 1
## 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
## 1 2 2 0 2 2 1 0 3 0 0 3 2 0 0 0
## 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640
## 0 0 3 0 3 1 3 2 1 3 3 0 1 1 3 2
## 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
## 3 1 0 3 0 2 0 2 0 1 1 1 1 2 1 3
## 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
## 1 3 2 2 1 3 2 0 1 3 0 3 3 0 2 1
## 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
## 1 2 0 3 2 0 3 2 3 0 0 3 0 2 2 3
## 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
## 2 2 2 2 1 1 3 0 1 1 1 2 1 0 0 1
## 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
## 0 0 3 0 1 1 0 1 1 1 3 0 3 2 3 0
## 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
## 0 1 2 2 1 0 1 1 0 1 1 0 0 3 3 0
## 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
## 3 1 2 3 0 1 0 2 2 0 3 1 0 3 0 1
## 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
## 0 3 3 3 2 3 0 3 2 0 1 0 3 3 2 0
## 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
## 2 1 3 1 0 3 2 0 3 1 2 1 1 1 3 1
## 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
## 1 1 2 0 0 2 2 0 2 0 0 0 0 3 3 3
## 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
## 3 0 1 2 2 1 0 0 2 1 0 2 0 2 2 2
## 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
## 1 2 0 2 1 3 0 0 3 1 3 0 0 2 3 2
## 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848
## 1 2 2 1 0 0 2 3 0 3 0 0 0 2 2 1
## 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
## 2 0 3 2 1 2 3 3 0 1 1 2 1 2 2 0
## 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
## 1 3 1 1 3 1 2 3 1 1 1 2 3 3 0 2
## 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
## 3 0 2 3 2 2 2 3 2 0 1 2 0 2 1 1
## 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
## 2 2 2 1 2 1 0 1 3 1 0 1 2 3 1 0
## 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
## 0 2 2 2 3 0 3 3 2 1 3 0 1 3 1 2
## 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
## 1 1 3 2 0 3 0 2 3 0 2 2 2 3 1 1
## 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
## 2 3 1 0 1 1 2 1 3 0 2 2 0 2 3 2
## 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
## 3 0 2 1 1 2 2 3 3 0 2 1 2 1 3 0
## 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
## 1 3 0 1 0 0 3 2 2 0 0 0 0 3 2 3
## 993 994 995 996 997 998 999 1000
## 3 0 0 2 1 0 2 2
## Levels: 0 1 2 3
Confusion matrix là một ma trận thể hiện số lượng điểm dữ liệu thuộc vào một class và được dự đoán thuộc vào class.
Confusion matrix cung cấp thêm thông tin về tỷ lệ phân lớp đúng giữa các lớp, hay giúp phát hiện các lớp có tỉ lệ phân lớp nhầm cao nhờ vào các khái niệm True (False) Positive (Negative).
Nguyễn Trọng Dũng: EDA và Trực quan hóa dữ liệu, Simple Linear Regression, Random Forest.
Đỗ Ngọc Hân: EDA và Trực quan hóa dữ liệu, SVM, K Mean Clustering.
Trần Thị Ngọc Trang: EDA và Trực quan hóa dữ liệu, Decision Tree.
Michael J. Crawley, The R Book - Second Edition, http://www.bio.ic.ac.uk/research/mjcraw/therbook/index.htm
Sarah Stowell, Using R for Statistics
W. N. Venables, D. M. Smith and the R Core Team, An Introduction to R
ABHISHEK SHARMA, Mobile Price Classification - Dataset Notebooks, https://www.kaggle.com/datasets/iabhishekofficial/mobile-price-classification/code
Wickham et al. ggplot2, https://ggplot2.tidyverse.org/
Wickham et al. Tidyverse, https://www.tidyverse.org/
Wickham et al. dplyr, https://dplyr.tidyverse.org/
Buuren et al. Package ‘mice’, https://cran.r-project.org/web/packages/mice/mice.pdf
Kaplan. Package ‘fastDummies’, https://cran.r-project.org/web/packages/fastDummies/fastDummies.pdf
Kuhn. The caret Package, March 27, 2019, https://topepo.github.io/caret/
Jarek Tuszynski, caTools, https://cran.r-project.org/web/packages/caTools/index.html
John Fox et al. carcar, https://cran.r-project.org/web/packages/car/index.html
Terry Therneau et al. rpart, https://cran.r-project.org/package=rpart
Alboukadel Kassambara ggpubr, https://rpkgs.datanovia.com/ggpubr/
Michael Hahsler et al. corrplot, https://cran.r-project.org/web/packages/corrplot/vignettes/corrplot-intro.html
Simon Garnier et al. viridis, https://cran.r-project.org/web/packages/viridis/index.html
David Meyer et al. e1071, https://cran.r-project.org/web/packages/e1071/index.html
Tobias Sing et al. ROCR, https://cran.r-project.org/web/packages/ROCR/index.html
Đỗ Ngọc Hân:
Công việc: EDA và Trực quan hóa dữ liệu, SVM, K Mean Clustering.
Mức độ hoàn thành: 100%.
Điểm: 10.
Trần Thị Ngọc Trang:
Công việc: EDA và Trực quan hóa dữ liệu, Decision Tree.
Mức độ hoàn thành: 100%.
Điểm: 10.
Nguyễn Trọng Dũng:
Công việc: EDA và Trực quan hóa dữ liệu, Simple Linear Regression, Random Forest.
Mức độ hoàn thành: 100%.
Điểm: 10.
Trần Thị Ngọc Trang:
Công việc: EDA và Trực quan hóa dữ liệu, Decision Tree.
Mức độ hoàn thành: 100%.
Điểm: 10.
Nguyễn Trọng Dũng:
Công việc: EDA và Trực quan hóa dữ liệu, Simple Linear Regression, Random Forest.
Mức độ hoàn thành: 100%.
Điểm: 10.
Đỗ Ngọc Hân:
Công việc: EDA và Trực quan hóa dữ liệu, SVM, K Mean Clustering.
Mức độ hoàn thành: 100%.
Điểm: 10.